package com.flexcloudsdk.taskqueue;

import java.util.ArrayList;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.TaskHandle;
import com.google.appengine.api.taskqueue.TaskOptions;

public class CloudQueue implements Queue{
	private static final Logger log = Logger.getLogger(CloudQueue.class
			.getName());
	private Queue queue = null;
	private String sourceClass;
	private String sourceMethod;

	public CloudQueue() {
		log.info("CloudQueue has been created by BlazeDS");
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudQueue.class.getName();
	}

	public CloudQueue(Queue value) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		
		if (value != null) {
			log.info("Queue value passed into contructor of CloudQueue: "
					+ value.getQueueName());
			queue = value;
		} else {
			log.severe("Queue is empty being passed into CloudQueue constructor");
		}
	}

	public TaskHandle add() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		TaskHandle handle = queue.add();
		new CloudTaskHandle(handle);
		return handle;
	}

	public TaskHandle add(TaskOptions value ) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		TaskHandle handle = queue.add(value);
		new CloudTaskHandle(handle);
		return handle;
	}

	public List<TaskHandle> add(Transaction txn,
		Iterable<TaskOptions> taskOptions) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		List<TaskHandle> handleList = queue.add(txn, taskOptions);
		List<CloudTaskHandle> cloudHandleList = new ArrayList<CloudTaskHandle>();
		if (!handleList.isEmpty()) {
			for (TaskHandle handle : handleList) {
				CloudTaskHandle cloudTask = new CloudTaskHandle(handle);
				cloudHandleList.add(cloudTask);
			}
		}
		return handleList;
	}

	public TaskHandle add(Transaction txn, TaskOptions taskoptions) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		TaskHandle handle = queue.add(txn, taskoptions);
		new CloudTaskHandle(handle);
	
		return handle;
	}

	public boolean deleteTask(TaskHandle handle) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		Boolean status = queue.deleteTask(handle);

		return status;
	}

	public boolean deleteTask(String taskName) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return queue.deleteTask(taskName);
	}

	public String getQueueName() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		return queue.getQueueName();
	}

	public void purge() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		queue.purge();
	}

	public List<TaskHandle> add(Iterable<TaskOptions> taskOptions) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		return null;
	}

	public List<Boolean> deleteTask(List<TaskHandle> taskHandles) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return null;
	}

	public List<TaskHandle> leaseTasks(long lease, TimeUnit unit,
			long countLimit) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return null;
	}

	public TaskHandle modifyTaskLease(TaskHandle taskHandle, long lease,
			TimeUnit unit) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		return null;
	}

}
